const Router = require('koa-router');
const router = new Router();
// 逻辑处理
// const login = require('../class/login');

// 首页
router.get('/', async (ctx, next) => {
    await ctx.render('index', {
        name: 'xin'
    })
})
//
router.get('/login', async (ctx, next) => {
    await ctx.render('login', {
        name: 'xin'
    })
})
// data
router.get('/test', async (ctx, next) => {
    let arr = []
    for (let i = 0; i < 50000; i++) {
        arr.push([Math.random() * 100, Math.random() * i])
    }
    ctx.body = JSON.stringify({
        status: 200,
        data: arr
    })
})

// data
router.get('/test1', async (ctx, next) => {
    let arr = []
    for (let i = 0; i < 50000; i++) {
        arr.push([Math.random() * 100, Math.random() * i])
    }
    ctx.body = JSON.stringify({
        status: 200,
        data: arr
    })
})

router.post('/regist', async (ctx, next) => {
    const { body } = ctx.request;
    // 调用数据库处理逻辑
    let res = await login.handNewUser(body);
    if (res) {
        ctx.body = JSON.stringify({
            status: 200,
            message: '注册成功!',
            token: res.token
        })
    } else {
        ctx.body = JSON.stringify({
            status: 500,
            message: '服务器内部错误!',
        })
    }
})

module.exports = router;